package com.gdupt.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gdupt.entity.ServiceManagement;
import com.gdupt.entity.ServiceUserAllot;
import com.gdupt.service.ICustomerInfoService;
import com.gdupt.service.IServiceManagementService;
import com.gdupt.service.IServiceUserAllotService;
import com.gdupt.service.ISystemUserService;
import com.gdupt.until.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务管理 服务管理 前端控制器
 * </p>
 *
 * @author gdupt
 * @since 2018-11-21
 */
@Controller
@RequestMapping("/service-management")
public class ServiceManagementController {
    @Autowired
    private IServiceManagementService serviceManagementService;
    @Autowired
    private ICustomerInfoService customerInfoService;
    @Autowired
    private ISystemUserService systemUserService;
    @Autowired
    private IServiceUserAllotService serviceUserAllotService;
    boolean flg=false;
 //服务创建
    @PostMapping("/insertService")
    public String insertService(ServiceManagement serviceManagement, HttpSession session){
       flg=serviceManagementService.insertService(serviceManagement);
        String msg="服务创建失败！";
        if (flg){
            msg="服务创建成功！";
        }
        session.setAttribute("msg",msg);
      return "redirect:/views/service_management/ServiceCreation.jsp";
    }
    //查询新创建的服务
    @RequestMapping("/findNewService")
    public @ResponseBody ResponseData finNewService(Page page,@RequestParam(value = "customerName",required = false) String customerName,
                                                    @RequestParam(value = "outline",required = false) String outline,@RequestParam(value = "serviceType",required = false) String serviceType){

        List<ServiceManagement> list = serviceManagementService.findNewService(page,customerName,outline,serviceType);
        return ResponseData.okPage(page.getTotal(),list);
    }
//获取系统用户名称
    @RequestMapping("/findRoleName")
    public @ResponseBody ResponseData findRoleName(){
        Map<String,Object> maps = new HashMap<>();
        maps.put("roles",systemUserService.list());
        return ResponseData.okData(maps);
    }
 //获取客户名称
    @RequestMapping("/findCustomerName")
    public @ResponseBody ResponseData findCustomerName(Model model){
        Map<String,Object> maps = new HashMap<>();
        maps.put("customers",customerInfoService.list());
        return ResponseData.okData(maps);
    }

}
